﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CWT.ArticleModule
{
    public class ArticleService : IArticleService
    {
        private ICwtFactory _factory;

        public ArticleService(ICwtFactory factory)
        {
            _factory = factory;
        }

        public bool SaveCategory(Category category)
        {
            using (CwtContext context = _factory.Get())
            {
                if (context.Set<Category>()
                     .Where(p => p.Id == category.Id)
                     .Select(p => p.Id).FirstOrDefault() == default(Guid))
                {
                    context.Set<Category>().Add(category);
                }
                else
                {
                    context.Entry(category).State = System.Data.EntityState.Modified;
                }
                context.SaveChanges();
                return true;
            }
        }

        public bool DeleteCategory(Category category)
        {
            using (CwtContext context = _factory.Get())
            {
                if (context.Set<Category>()
                     .Where(p => p.Id == category.Id)
                     .Select(p => p.Id).FirstOrDefault() == default(Guid))
                {
                    context.Set<Category>().Remove(category);
                }
                else
                {
                    context.Entry(category).State = System.Data.EntityState.Deleted;
                }
                context.SaveChanges();
                return true;
            }
        }

        public Category FindCategory(string code)
        {
            using (CwtContext context = _factory.Get())
            {
                return context.Set<Category>().Where(p => p.Code == code).FirstOrDefault();
            }
        }

        public List<Category> FindCategories()
        {
            using (CwtContext context = _factory.Get())
            {
                return context.Set<Category>().ToList();
            }
        }

        public bool SaveArticle(Article article)
        {
            using (CwtContext context = _factory.Get())
            {
                if (context.Set<Article>()
                     .Where(p => p.Id == article.Id)
                     .Select(p => p.Id).FirstOrDefault() == default(Guid))
                {
                    context.Set<Article>().Add(article);
                }
                else
                {
                    context.Entry(article).State = System.Data.EntityState.Modified;
                }
                context.SaveChanges();
                return true;
            }
        }

        public List<Article> FindArticles(Guid categoryId, int start, int limit)
        {
            using (CwtContext context = _factory.Get())
            {
                return context.Set<Article>()
                    .Where(p => p.CategoryId == categoryId)
                    .OrderBy(p=>p.PostedDate)
                    .Skip(start)
                    .Take(limit)
                    .ToList();
            }
        }

        public List<Article> FindArticles(string categoryCode, int start, int limit)
        {
            using (CwtContext context = _factory.Get())
            {
                return context.Set<Article>()
                    .Where(p => p.Category.Code == categoryCode)
                    .OrderBy(p => p.PostedDate)
                    .Skip(start)
                    .Take(limit)
                    .ToList();
            }
        }
    }
}
